package com.bawei.module_apps.net

import androidx.lifecycle.viewModelScope
import com.bawei.library_mvi.model.BaseViewModel
import com.bawei.module_apps.net.model.NetAppService
import com.bw.apps.net.entity.AppEntity
import com.bw.apps.net.entity.AppMultipleEntity
import com.bw.network.download.DownloadManager
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch

/**
 * @Author : 
 * @Time : On 2023/7/21 10:23
 * @Description : NetAppViewModel
 */
class NetAppViewModel:BaseViewModel<NetAppIntent>(){
    private val service:NetAppService by lazy{NetAppService()}
    private val _states = MutableStateFlow<NetAppState>(NetAppState.Init)//MutableStateFlow可变
    val states: StateFlow<NetAppState>
        get() = _states


    //处理意图
    override fun handleIntent() {
        viewModelScope.launch {
            intents.consumeAsFlow().collect{
                when(it){
                    is NetAppIntent.NetAppDataIntent -> getNetAppData(it)
                    is NetAppIntent.NetAppDownloadIntent -> downloadApp(it)
                }
            }
        }

    }
    //下载app
    private fun downloadApp(it: NetAppIntent.NetAppDownloadIntent) {
        viewModelScope.launch {
            DownloadManager.download(it.url,it.file)
                .onStart {
                    _states.value = NetAppState.Start //开始下载
                }
                .collect{
                //直接更新下载的状态
                _states.value = it
            }
        }
    }

    //获得网络数据更新UI
    private fun getNetAppData(netAppIntent: NetAppIntent.NetAppDataIntent) {
        viewModelScope.launch {
            flow {
                val focus = service.requestApps(netAppIntent.requestEntity)
                emit(focus)
            }.catch{
                e-> _states.value = NetAppState.ErrorState//失败
            }.collect{

                val entity = it as AppEntity
                //转换------
                val list = ArrayList<AppMultipleEntity>()
                entity.data.forEach {
                    val titleEntity = AppMultipleEntity(it.title,0,"","",0.0,
                        0,"",0,0,
                        true,true,"","","","","",
                        "","",NetAppsAdapter.TITLETYPE)
                    list.add(titleEntity)
                    var index = 0
                    while (index<it.appsList.size){
                        list.add(AppMultipleEntity("",it.appsList[index].appId,
                            it.appsList[index].appIcon,it.appsList[index].appName,
                            it.appsList[index].appSize, it.appsList[index].appUserId,
                            it.appsList[index].userName,it.appsList[index].uploadTime,
                            it.appsList[index].updateTime,it.appsList[index].operationFlag,
                            it.appsList[index].trustFlag,it.appsList[index].apkUrl,
                            it.appsList[index].appImgs,it.appsList[index].versionCode,
                            it.appsList[index].appIntroduction,it.appsList[index].updateStr,
                            it.appsList[index].netUrl,it.appsList[index].app_package,
                            NetAppsAdapter.VALUETYPE))
                        index ++
                    }
                }
                _states.value = NetAppState.SucceedState(list)//成功
            }

        }
    }
}